/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @param <Type>
 */
public class Loop<Type> {

      private List<Type> list;
      private Integer index;

      /**
       *
       */
      public Loop () {
            this.list = new ArrayList<> ( 100 );
            this.index = -1;
      }

      /**
       *
       * @param array
       */
      public Loop ( Type... array ) {
            this.list = Arrays.asList ( array );
            this.index = -1;
      }

      /**
       *
       * @param type
       * @return
       */
      public Loop<Type> add ( Type type ) {
            list.add ( type );
            return this;
      }

      /**
       *
       * @param array
       * @return
       */
      public Loop<Type> add ( Type[] array ) {
            for ( Type t : array ) {
                  this.add ( t );
            }
            return this;
      }

      /**
       *
       * @param type
       * @return
       */
      public Boolean has ( Type type ) {
            return this.list.contains ( type );
      }

      /**
       *
       * @param type
       * @return
       */
      public Loop<Type> del ( Type type ) {
            this.list.remove ( type );
            return this;
      }

      /**
       *
       * @return
       */
      public Type loop () {
            Integer size = list.size ();
            if ( index >= size - 1 ) {
                  index = 0;
            } else {
                  index += 1;
            }
            return list.get ( index );
      }

      @Override
      public boolean equals ( Object obj ) {
            if ( obj == null ) {
                  return false;
            }
            if ( getClass () != obj.getClass () ) {
                  return false;
            }
            @SuppressWarnings ( "unchecked" )
            final Loop<Type> other = ( Loop<Type> ) obj;
            if ( this.list != other.list && ( this.list == null ||  ! this.list.equals ( other.list ) ) ) {
                  return false;
            }
            return true;
      }

      @Override
      public int hashCode () {
            int hash = 7;
            hash = 19 * hash + ( this.list != null ? this.list.hashCode () : 0 );
            return hash;
      }

      @Override
      public String toString () {
            return "EzLoop" + list;
      }

}
